package com.greate.community.controller;

import com.greate.community.entity.Section;
import com.greate.community.entity.User;
import com.greate.community.service.SectionService;
import com.greate.community.util.BusCodeEnum;
import com.greate.community.util.CommunityConstant;
import com.greate.community.util.CommunityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/section")
public class SectionController implements CommunityConstant {

    @Autowired
    private SectionService sectionService;

    @GetMapping("/getManagement")
    public String getSectionManagement(Model model){
        List<Section> sections = sectionService.getAllSectionsWithUser();
        for (Section section:sections){
            System.out.print(section);
        }
        model.addAttribute("sections", sections);
        return "/site/admin/sectionManagement";
    }

    @PostMapping("/changeStatus")
    public String changeStatus(Model model, String sectionId, String status){
        sectionService.updateStatus(Integer.parseInt(sectionId),Integer.parseInt(status));
        List<Section> sections = sectionService.getAllSectionsWithUser();
        model.addAttribute("sections", sections);
        return "/site/admin/sectionManagement::dataContent";
    }

    @PostMapping("/searchSections")
    public String searchSections(Model model, String sectionName, String username, String status, String startTime, String endTime){
        List<Section> sections = sectionService.getSectionsByParams(sectionName, username, status, startTime, endTime);
        model.addAttribute("sections", sections);
        return "/site/admin/sectionManagement::dataContent";
    }

    @PostMapping("/changeModerator")
    @ResponseBody
    public String changeModerator(String sectionId, String userId){
        sectionService.changeModerator(sectionId, userId);
        return CommunityUtil.getJSONString(BusCodeEnum.SUCCESS.getCode());
    }

    @RequestMapping("/addSection")
    public String addSection(String name){
        sectionService.addSection(name);
        return "/site/admin/sectionManagement::dataContent";
    }
}
